var appPath = getAppPath();

//初始化加载数据
$(function(){
	var url = appPath + "/itbOrders/getExpClassificationInfo.action";
	$("#expType").combobox({//稍等
		url:url,
		valueField : 'exc_id',
		textField : 'name',
		onLoadSuccess:function(){
			$("#expType").combobox('select',0).combobox('enable');
		}
	});
	
	var data = [{'id':-1,'name':'不限'},{'id':0,'name':'未知'},
	            {'id':1,'name':'揽件中'},{'id':2,'name':'在途中'},
	            {'id':3,'name':'派件中'},{'id':4,'name':'已签收'},
	            {'id':5,'name':'拒签'},{'id':6,'name':'问题件'},
	            {'id':7,'name':'退回'}];
	$("#expState").combobox({
		data : data,
		valueField : 'id',
		textField : 'name',
		onLoadSuccess:function(){
			$("#expState").combobox('select',-1).combobox('enable');
		}
	});
	
	initJspGrid();
});




//查询物流信息
function doQuery(){
	$.loading(true);
	var expType = $("#expType").combobox('getValue');//获取订单类型
	var expNum = $("#queryExpNum").val();//获取订单号
	if(expType.length<=0){
		$.messager.alert('提示信息','订单类型不对,请联系管理员！','info');
		return;
	}
	if(expNum.length<=0){
		$.messager.alert('提示信息','订单号不能为空','info');
		return;
	}
	var data = {
			"number" :expNum,
			"type" :expType
	};
	
	var url = appPath + "/itbOrders/getExpressInfo.action";//物流订单信息
	$.post(url,data,function(ret){//post区分大小写
		 var status = ret.status;//返回状态
		 if(status==0){
			 alert("查询成功");
			 var eNumber = ret.result.number;
			 $("#epNumber").html(eNumber);
			 var arr = ret.result.list;
			 var htm = '';
			 for(var i=0;i<arr.length;i++){
				 var info = arr[i];
				 var time = info.time;
				 var status = info.status;
				 htm = htm +'<tr><td class="queryTitle" style="width:68px;">时间</td><td class="queryContent" style="width:120px;">'+time+'</td>'+
				 '<td class="queryTitle" style="width:68px;">快递地点:</td><td class="queryContent">'+status+'</td></tr>'; 
			 }
			 var tmp = '<tr><td class="queryTitle" style="width:68px;">物流单号:</td>'+
			 '<td class="queryTitle" align="center">'+eNumber+'</td></tr>';
			 $("#expTable").html(tmp+htm);
			 $.loading(false);
		 }else{
			 var tmp = '<h3 style="width:547px;" align="center">暂无物流信息</h3>';
			 $("#expTable").html(tmp);
			 $.loading(false);
		 }
	});
}

//初始化加载本地订单
function initJspGrid(){
	$('#expTacTable').datagrid({
	    url:appPath + "/itbOrders/getLogisticsInfo.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[	     
	        {field:'ex_id',title:'信息ID',width:30,align:'center',sortable:false},
	        {field:'source',title:'来源',width:40,align:'center',sortable:false,
	         	  formatter:function(value, rowData, rowIndex){
	         		  var ret = '<span>未知</span>';
	        		  switch(value||'0'){
		        		  case 0: ret = '<span  style="color:#FF8247;">未知</span>';break;
		        		  case 10: ret = '<span style="color:#FF83FA;">世界港</span>';break;
		        		  case 20: ret = '<span style="color:green;">阿里云</span>';break;
	        		  }
	        		  return ret;
	        	  }
	        },
//	        {field:'exc_id',title:'快递公司ID',width:30,align:'center',sortable:false},	      
	        {field:'exp_type',title:'快递公司',width:45,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		if(rowData.exc_id){
	        			return matChExpName(rowData.exc_id);
	        		}
	        	}
	        },	      
	        {field:'ex_tracking_no',title:'物流单号',width:60,align:'center',sortable:false},
	        {field:'trade_obj_id',title:'订单ID',width:30,align:'center',sortable:false,
		  	   	  formatter:function(value, rowData, rowIndex){ 	
		  	   		  if(value){
		  	   			  return '<span style="color:blue">'+value+'</span>';
		  	   		  }
		  	   		  return '<span style="color:blue">暂无</span>';;
			  	  }
	        },
	        {field:'trade_type',title:'订单类型',width:50,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){ 	
	        		var ret = '<span>未知</span>';
	        		  switch(value||'0'){
		        		  case 10: ret = '<span>商品订单</span>';break;
		        		  case 20: ret = '<span>发票</span>';break;
		        		  case 30: ret = '<span>购买模板</span>';break;
		        		  case 40: ret = '<span>虚拟商品</span>';break;
		        		  case 50: ret = '<span>秒杀订单</span>';break;
	        		  }
	        		return ret;
	        	}
	        },
	        {field:'state',title:'快递状态',width:50,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		var ret = '<span style="color:#B5B5B5;">未知</span>';
	        		  switch(value||'0'){
		        		  case 0: ret = '<span style="color:#B5B5B5;">未知</span>';break;
		        		  case 1: ret = '<span style="color:#FF34B3;">揽件中</span>';break;
		        		  case 2: ret = '<span>已揽件</span>';break;
		        		  case 3: ret = '<span style="color:#32CD32;">派件中</span>';break;
		        		  case 4: ret = '<span style="color:#FF4040;">已签收</span>';break;
		        		  case 5: ret = '<span>派件失败</span>';break;
		        		  case 6: ret = '<span>问题件</span>';break;
		        		  case 7: ret = '<span>退回</span>';break;
	        		  }
	        		return ret;
	        	}
	        },
	        {field:'data',title:'详情',width:40,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		var ret = '<span>暂无</span>';
	        		if(value){
	        			ret = '<a href="javascript:void(0)" onclick="showDetil('+JSON.stringify(value).replace(/"/g, '&quot;')+')">查看</a>';
	        		}
	        		return ret;
	        	}
	        },
	        {field:'udp',title:'物流更新',width:40,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		var ret = '<a href="javascript:void(0)" onclick="updExp('+rowData.ex_id+',\''+rowData.ex_tracking_no+'\','+rowData.exc_id+')">更新</a>';
	        		return ret;
	        	}
	        },
	        {field:'create_time',title:'创建时间',width:80,align:'center',sortable:false},
	        {field:'update_time',title:'修改时间',width:80,align:'center',sortable:false}
	    ]],
	    pagination:true,
		rownumbers:true,
		toolbar:[
			'-',
					{
					text:'<font color="red">批量更新物流</font>',
					iconCls:'icon-reload',
					handler:function(){
						doUpdate();
					}
				},
			'-'
			]
	});
}

// 批量更新
function doUpdate(){
	$.messager.confirm("确认",
			"<span style='color:blue'>确定批量更新所有【配送中】的订单?</span>",
			function(r){
		        if (r) {
//		        	var timeout=setTimeout(function(){  
//						$.loading(false);
//						$.messager.alert('提示信息','<span style="color:red;">物流信息正在自动刷新中</span>','info');
//					  },  
//					  5000 //超时时间，考虑到网络问题，5秒还是比较合理的  
//					); 
//						if(timeout){ //清除定时器  
//						    clearTimeout(timeout);  
//						    timeout=null;  
//						} 
//							var cnt = ret.data.cnt||'0';
//							$.messager.alert('提示信息','完成更新,已更新'+cnt+'条物流信息','info');
//		        	var url = "http://localhost:3000/callFunc/updExpTrackData?key=1&force=0&time=2";
		        	
		        	$.loading(true);
		        	var url = getApiServer()+"/callFunc/updExpTrackData?key=1&force=0&time=2";
					$.get(url,function(ret){
						$.loading(false);
						if(ret && ret.success==1){
							$.messager.alert('提示信息',ret.data.msg,'info');
						} else {
							$.messager.alert('提示信息','请求失败','info');
						}
					});
		        }
			}
	);
}


// 更新单个物流信息
function updExp(ex_id,num,type){
	$.messager.confirm('确定','是否确认更新物流信息',function(r){
		if(r){
			$.loading(true);
			var url = appPath +"/itbOrdersWeb/updExpInfoData.action";
			var data = {
					'ex_id' :ex_id,
					'number' :num,
					'type' :matchExpType(type)
			};
//			var timeout=setTimeout(function(){  
//					$.loading(false);
//					$.messager.alert('提示信息','<span style="color:red;">物流信息正在自动刷新中</span>','info');
//				  },  
//				  5000 //超时时间，考虑到网络问题，5秒还是比较合理的  
//				);  
//				if(timeout){ //清除定时器  
//				    clearTimeout(timeout);  
//				    timeout=null;  
//				} 
			
			$.post(url,data,function(ret){
				if(ret == 'ok'){
					$.loading(false);
					$.messager.alert('提示信息','物流信息更新成功','info');
				}else if(ret =='noVail'){
					$.loading(false);
					$.messager.alert('提示信息','暂无物流信息,检查物流单号或者物流公司！','info');
				}else if(ret == 'null'){
					$.loading(false);
					$.messager.alert('提示信息','物流信息暂时没有更新,晚点再做更新！','info');
				}else{
					$.loading(false);
					$.messager.alert('提示信息','更新失败','info');
				}
			});
		}
	});
}

function matChExpName(type){
	var st = '<span>未知</span>';
	if(type){
		switch(type){
			case 1: st ='<span>顺丰</span>';break;
			case 5: st ='<span>百世汇通</span>';break;
			case 6: st ='<span>百世快运</span>';break;
			case 10: st ='<span>中通快递</span>';break;
			case 11: st ='<span>中通快运</span>';break;
			case 15: st ='<span>申通</span>';break;
			case 16: st ='<span>圆通</span>';break;
			case 20: st ='<span>韵达</span>';break;
			case 30: st ='<span>邮政包裹</span>';break;
		}
	}
	return st;
}


/**
 * 匹配快递类型
 */
function matchExpType(type){
	var st = '';
	if(type){
		switch(type){
			case 1: st ='SFEXPRESS';break;
			case 5: st ='HTKY';break;
			case 6: st ='BSKY';break;
			case 10: st ='ZTO';break;
			case 11: st ='ZTO56';break;
			case 15: st ='STO';break;
			case 16: st ='YTO';break;
			case 20: st ='YUNDA';break;
			case 30: st ='CHINAPOST';break;
		}
	}
	return st;
}

function showDetil(ret){
	$('#expInfoDlg').window({
		title:'物流详情',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#expInfoDlg').window('move',{top:20,left:20});
	$('#expInfoDlg').window('open');
	 var ret = $.parseJSON(ret);
	 var status = ret.status;//返回状态
	 if(status){
		 var eNumber = ret.result.number;
		 $("#epNumber").html(eNumber);
		 var arr = ret.result.list;
		 var htm = '';
		 for(var i=0;i<arr.length;i++){
			 var info = arr[i];
			 var time = info.time;
			 var status = info.status;
			 htm = htm +'<tr><td class="queryTitle" style="width:68px;">时间</td><td class="queryContent" style="width:120px;">'+time+'</td>'+
			 '<td class="queryTitle" style="width:68px;">快递地点:</td><td class="queryContent">'+status+'</td></tr>'; 
		 }
		 var tmp = '<tr><td class="queryTitle" style="width:68px;">物流单号:</td>'+
		 '<td class="queryTitle" align="center">'+eNumber+'</td><td class="queryTitle" style="width:68px;"></td><td class="queryTitle" align="center"></td></tr>';
		 $("#expTable").html(tmp+htm);
	 }else{
		 var tmp = '<h3 style="width:547px;" align="center">暂无物流信息</h3>';
		 $("#expTable").html(tmp);
	 }
}

//查询订单物流
function doQuery2(stat){
	$("#expTacTable").datagrid('unselectAll');
	if(stat==1){
		$("#expTacTable").datagrid('load',getQueryParam());
	}else{
		$("#expTacTable").datagrid('reload',getQueryParam());
	}
}

function getQueryParam(){
	var obj = {};
	var number = $("#queryExpNum").val();
	var type = $("#expType").combobox('getValue');
	var state = $("#expState").combobox('getValue');
	
	if(number) obj.findNum = number;
	if(type) obj.findExpType = type;
	if(state && state!=-1) obj.findExpState = state;
	return obj;
}